package com.hjl.sr;

import org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerRequestExecutor;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;

import com.hjl.sr.remoting.SecurityCommonsHttpInvokerRequestExecutor;
import com.hjl.sr.remoting.SecuritySimpleHttpInvokerRequestExecutor;
import com.hjl.sr.security.BlowFishEncrypter;
import com.hjl.sr.security.Encrypter;
import com.hjl.sr.security.PrefixTokenEncrypter;
import com.hjl.sr.security.SecurityUtil;
import com.hjl.sr.security.token.ClientTokenManager;
import com.hjl.sr.security.token.TokenManager;
import com.hjl.sr.security.token.TokenService;

/**
 * @author pister
 * 2012-12-17 下午08:36:14
 */
public class SecurityHttpInvokerProxyFactoryBean extends HttpInvokerProxyFactoryBean {

	private String encryptKey = SecurityUtil.DEFAULT_ENCRYPT_KEY;
	
	private TokenManager tokenManager;
	
	private TokenService tokenService;
	
	public HttpInvokerRequestExecutor getHttpInvokerRequestExecutor() {
		HttpInvokerRequestExecutor ret = super.getHttpInvokerRequestExecutor();
		if (ret instanceof SimpleHttpInvokerRequestExecutor) {
			SecuritySimpleHttpInvokerRequestExecutor executor = new SecuritySimpleHttpInvokerRequestExecutor(getEncrypter());
			executor.setBeanClassLoader(getBeanClassLoader());
			setHttpInvokerRequestExecutor(executor);
			return executor;
		} else if (ret instanceof CommonsHttpInvokerRequestExecutor) {
			SecurityCommonsHttpInvokerRequestExecutor executor = new SecurityCommonsHttpInvokerRequestExecutor(getEncrypter());
			executor.setBeanClassLoader(getBeanClassLoader());
			setHttpInvokerRequestExecutor(executor);
			return executor;
		}
		throw new RuntimeException("unsupport HttpInvokerRequestExecutor :" + ret);
	}


	public TokenManager getTokenManager() {
		if (tokenManager == null) {
			tokenManager = new ClientTokenManager(tokenService);
		}
		return tokenManager;
	}


	public void setTokenManager(TokenManager tokenManager) {
		this.tokenManager = tokenManager;
	}


	public Encrypter getEncrypter() {
		BlowFishEncrypter blowFinshEncrypter = new BlowFishEncrypter(encryptKey);
		return new PrefixTokenEncrypter(blowFinshEncrypter, getTokenManager());
	}

	public void setEncryptKey(String encryptKey) {
		this.encryptKey = encryptKey;
	}

	public void setTokenService(TokenService tokenService) {
		this.tokenService = tokenService;
	}
	
	public void setTokenServiceUrl(String tokenServiceUrl) {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(tokenServiceUrl);
		httpInvokerProxyFactoryBean.setServiceInterface(TokenService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		this.tokenService = (TokenService)httpInvokerProxyFactoryBean.getObject();
	}

}
